<?php
/*
 * To control the New Post
 * *****NOTE: (Default) uid = "4cb6c26f6792ff3015000000"
 * 
 */
class Community_NewpostController extends Zend_Controller_Action
{

    public function init()
    {
         // Khởi tạo các thẻ [Lĩnh vực tìm kiếm]
        $category=new Admin_Model_MCategory();
        $bookCategory=$category->getAllCategories(array("_id","name"));
        $this->view->category=$bookCategory;

        // Cài đặt thông tin đăng nhập
        $mySession=new Zend_Session_Namespace();
        if(isset ($mySession->uid) && isset($mySession->username))
        {
            $this->view->username=$mySession->username;
            $this->view->uid=$mySession->uid;

            $userModel=new Personal_Model_MUser();
            $this->view->userinfo=$userModel->getInfoBasic($mySession->uid);
        }
        
    }

    public function indexAction()
    {
        $this->_helper->layout()->setLayout('layout_community');
        $mySession=new Zend_Session_Namespace();
        if(!isset ($mySession->uid) || !isset($mySession->username))
        {
            $this->_redirect('/default/index/login');
            return;
        }

        $request=$this->getRequest();
        $type=$request->getParam('type');
        $this->view->type=$type;
    }

    public function addpostAction()
    {
        $this->_helper->viewRenderer->setNoRender();

        // kiem tra da dang nhap chua
        $mySession=new Zend_Session_Namespace();
        if(!isset ($mySession->uid) || !isset($mySession->username))
        {
            $this->_redirect('/default/index/login');
            return;
        }

        $request=$this->getRequest();
        $type=$request->getParam('type');   // lay loai bai post : event hoac story
        $title=$request->getParam('titlepost'); // lay ten bai viet
        $content=$request->getParam('contentpost'); // lay noi dung
        $title=$this->standardstring($title);       // chuan hoa ten bai viet
        $content=$this->standardstring($content);   // chuan hoa noi dung
        if(strlen($title)==0 || strlen($content)==0)  // kiem tra noi dung ten bai hoac noi dung ko hop le
        {
            $this->_redirect("");
            return;
        }

        // them moi event
        if($type=='event')
        {
            $eventModel=new Community_Model_MEvent();
            $eid=$eventModel->addEvent($mySession->uid, $title, $content);
            if($eid !=FALSE)
            {
                $this->_redirect("/community/event/detail?id=".$eid);
                return;
            }
            else
            {
                $this->_redirect("");
                return;
            }
        }
        // them moi bai viet
        else if($type=='story')
        {
            $storyModel=new Community_Model_MStory();
            $sid=$storyModel->addStory($mySession->uid, $title, $content);
            if($sid !=FALSE)
            {
                $this->_redirect("/community/story/detail?id=".$sid);
                return;
            }
            else
            {
                $this->_redirect("");
                return;
            }
        }
        else
        {
            $this->_redirect("");
        }
    }

    
    
    private function standardstring($str)
    {
        $i=0;
        while($i< strlen($str))
        {
            if($str[$i]==' ' && ($i==0 || $i==strlen($str)-1 || $str[$i+1]==' '))
            {
                if($i>0 && $i<strlen($str)-1)
                {
                    $str=substr($str, 0,$i).  substr($str, $i+1);
                }
                else if($i==0)
                    $str=substr($str, 1);
                else if($i==strlen($str)-1)
                    $str=substr($str, 0,strlen($str)-1);
            }
            else
                $i++;
        }

        return $str;
    }
}

